<?php
namespace Managers;

class Mail extends BaseManager
{
    public function addSubscriber($mail) //Добавляет подписчика
    {
        $Subscriber = new \Catalog\Subscription();
        $Subscriber->setMail($mail);

        $this->_em->persist($Subscriber);
        $this->_em->flush();
    }

    public function isSubscriber($mail)
    {
        $result = $this->_em->createQueryBuilder()
                ->select('s')
                ->from('\Catalog\Subscription', 's')
                ->where('s.mail = :mail')->setParameter('mail', $mail)
                ->getQuery()
                ->getArrayResult();
        if (count($result) == 0) return false;
        else return $result[0];
    }

    public function sendMail($text, $subject, $who_mail, $who_name, $whom_mail, $whom_name)
    {
        $mail = new \Zend_Mail();
        $mail->setBodyHtml($text);
        $mail->setFrom($who_mail, $who_name);
        $mail->addTo($whom_mail, $whom_name);
        $mail->setSubject($subject);
       // die('tyt');//пока пусть умирает, а то на локалке не отправляет
        $mail->send();

    }

    public function addSearchToMail($user, $tag, $time)
    {
        $searchMail = new \Catalog\Searchtomail();




        $searchMail->setUser( $this->_em->getReference('\Catalog\User', $user) );
        $searchMail->setTag( $this->_em->getReference('\Catalog\Tags', $tag) );
        $searchMail->setTime($time);

        $this->_em->persist($searchMail);
        $this->_em->flush();
    }

    public function getSearchToMail($user, $tag)
    {
        $result = $this->_em->createQueryBuilder()
                ->select('s')
                ->from('\Catalog\Searchtomail', 's')
                ->where('s.user = :user')->setParameter('user', $user)
                ->andWhere('s.tag = :tag')->setParameter('tag', $tag)
                ->getQuery()
                ->getArrayResult();
        if (count($result) == 0) return false;
        else return $result[0];
    }

    public function allSearchMailList($user)
    {
        $result = $this->_em->createQueryBuilder()
                ->select('s', 'u.id as user_id', 't.name as tag_name', 't.id as tag_id')
                ->from('\Catalog\Searchtomail', 's')
                ->join('s.user', 'u')
                ->join('s.tag', 't')
                ->where('s.user = :user')->setParameter('user', $user)
                ->getQuery()
                ->getArrayResult();
        if (count($result) == 0) return false;
        else return $result;
    }

    public function deleteSearchTag($id)
    {
        $entity = $this->_em->find('\Catalog\Searchtomail', $id); //достало по айпишке то что будем менять
        if ($entity) {
            $this->_em->remove($entity);
            $this->_em->flush();
        }
    }

    public function upMailTime($user, $time)
    {
        $userTags = $this->allSearchMailList($user);
        if (!empty($userTags))
            for ( $i=0; $i<count($userTags); $i++ ):
                $id = $userTags[$i][0]['id'];

                $timeUp = $this->_em->find('\Catalog\Searchtomail', $id);
                $timeUp->setTime($time);
                $this->_em->persist($timeUp);
                $this->_em->flush();
            endfor;
    }

    public function IsMail($user, $old_mail)
    {
        $result = $this->_em->createQueryBuilder()
                ->select('u')
                ->from('\Catalog\User', 'u')
                ->where('u.id =:id')->setParameter('id', $user)
                ->andWhere('u.email =:email')->setParameter('email', $old_mail)
                ->getQuery()
                ->getArrayResult();
        if (count($result) == 0) return false;
        else return $result;
    }

    public function ExchangeMail($user, $new_mail)
    {
        $mailUp = $this->_em->find('\Catalog\User', $user);
        $mailUp->setEmail($new_mail);
        $this->_em->persist($mailUp);
        $this->_em->flush();
    }
}